package com.atguigu.gmall.util.service.impl;

import com.atguigu.gmall.common.constant.RedisConst;
import com.atguigu.gmall.common.util.MD5;
import com.atguigu.gmall.util.service.RabbitService;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;

import java.io.IOException;

/**
 * @author:ahang
 * @create:2022-10-27 22:51
 * @Description:
 */
@Slf4j
public class RabbitServiceImpl implements RabbitService {

    @Autowired
    StringRedisTemplate redisTemplate;

    public void retry(int count, String msgContent, Channel channel,Long deliveryTag) throws IOException {
        String md5 = MD5.encrypt(msgContent);
        Long increment = redisTemplate.opsForValue().increment(RedisConst.MQ_RETRY + md5);
        if(increment < count){
            log.info("消费消息失败");
            channel.basicNack(deliveryTag,false,true);
        }else{
            log.info("消费消息超过最大次数");
            redisTemplate.delete(RedisConst.MQ_RETRY + md5);
        }
    }
}
